package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.google.zxing.WriterException;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.module.mine.model.UserAsset;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletChangeUnfreezeContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.push.model.PushBean;
import com.meitianhui.convenitenbuy.util.JumpUtil;
import com.meitianhui.convenitenbuy.util.LogUtils;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.util.ZXingUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletChangeUnfreezeActivity extends BaseToolbarActivity implements WalletChangeUnfreezeContract.View {
    private UserAsset bean;
    private ImageView mIvCode;

    @Override
    protected void initView(Bundle savedInstanceState) {
        bean = getIntent().getParcelableExtra(JumpUtil.JUMP_WALLET_FRAGMENT_VALUE);
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        isEnable(false);
        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_change_unfreeze_title));
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_change_unfreeze;
    }

    /**
     * 初始化Id
     */
    private void initId() {
        mIvCode = getViewById(R.id.iv_wallet_payment_unfreeze);
        registerPush();
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        try {
            mIvCode.setImageBitmap(ZXingUtils.createQRCode(InfoProvider.getUserToken(this).getMember_id(), 200));
            Animation a = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
            mIvCode.setAnimation(a);
        } catch (WriterException e) {
            LogUtils.e("create unfreeze qr code fail!");
        }
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
        }
    }

    @Override
    protected void onRefreshData() {
    }

    @Override
    public void showError(String msg) {

    }

    private void registerPush() {
        Disposable disposable = RxBus.get().toFlowable(PushBean.class).subscribe(new Consumer<PushBean>() {
            @Override
            public void accept(PushBean obj) throws Exception {
                if (obj != null) {
                    if (obj.getType().equals(Constants.PUSH_TYPE_UNFREEZE_SUCCESS)) {
                        TipUtils.showToast(WalletChangeUnfreezeActivity.this, getString(R.string.wallet_change_unfreeze_success), 1);
                        finish();
                    }
                }
            }
        });
        mCompositeDisposable.add(disposable);
    }

}

